﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.DAL;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;

namespace ZhouYu.NewWorld.BLL.Service
{
    public class HotelService : BaseRepository<Hotel>, IHotelService
    {
        /// <summary>
        /// 多表添加
        /// </summary>
        /// <param name="hotel"></param>
        /// <param name="hotelDetails"></param>
        /// <returns></returns>
        public ResponseResult BatchAdd(HotelTemporary temporary)
        {
            ResponseResult result = new ResponseResult();
            //创建实例
            using (var db = new SyDbContext())
            {
                //开启事务
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //酒店
                        Hotel hotel = new Hotel();
                        hotel.HotelName = temporary.HotelName;
                        hotel.EndPlace = temporary.EndPlace;
                        hotel.TotelType = temporary.TotelType;
                        hotel.Level = temporary.Level;
                        hotel.Hotel_Tel = temporary.Hotel_Tel;
                        hotel.Describe = temporary.Describe;
                        hotel.Hotel_CreateTime = DateTime.Now;
                        hotel.Hotel_UpdateTime = DateTime.Now;
                        hotel.HotelId = temporary.HotelId;
                        //添加酒店表-- db.表名.Add传入对象
                        db.Hotel.Add(hotel);
                        //保存
                        db.SaveChanges();

                        //酒店详细
                        HotelDetails hotelDetails = new HotelDetails();
                        hotelDetails.HotelId = hotel.HotelId;
                        hotelDetails.HotelAddress = temporary.HotelAddress;
                        hotelDetails.NumberOfRooms = temporary.NumberOfRooms;
                        hotelDetails.Hotel_BigPic = temporary.Hotel_BigPic;
                        hotelDetails.Hotel_SmallPic = temporary.Hotel_SmallPic;
                        hotelDetails.HotelDetails_CreateTime = DateTime.Now;
                        hotelDetails.HotelDetails_UpdateTime = DateTime.Now;
                        //添加酒店详细表
                        db.HotelDetails.Add(hotelDetails);
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                        result.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        result.Success = false;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 分页多表查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public ResponseResult GetHotelInfo(int page, int limit)
        {
            ResponseResult responseResult = new ResponseResult();
            //创建实例            
            using (var db = new SyDbContext())
            {
                //两表连接查询
                var result = from a in db.Hotel
                             join b in db.HotelDetails
                             on a.HotelId equals b.HotelId
                             select new HotelTemporary() //自定义实体类 包括需要展示的字段
                             {
                                 //赋值
                                 HotelId = a.HotelId,
                                 HotelDetails_Id = b.HotelDetails_Id,
                                 HotelName = a.HotelName,
                                 EndPlace = a.EndPlace,
                                 Hotel_Tel = a.Hotel_Tel,
                                 TotelType = a.TotelType,
                                 Level = a.Level,
                                 Describe = a.Describe,
                                 HotelAddress = b.HotelAddress,
                                 NumberOfRooms = b.NumberOfRooms,
                                 Hotel_BigPic = b.Hotel_BigPic,
                                 Hotel_SmallPic = b.Hotel_SmallPic
                             };
                //responseResult对象赋值
                responseResult.data = result.ToList().Skip((page - 1) * limit).Take(limit);
                responseResult.count = result.ToList().Count;
                responseResult.msg = string.Empty;
                responseResult.code = 0;
            }
            return responseResult;
        }

        /// <summary>
        /// 多表编辑
        /// </summary>
        /// <param name="hotel"></param>
        /// <param name="hotelDetails"></param>
        /// <returns></returns>
        public ResponseResult UpdateHotel(HotelTemporary temporary)
        {
            ResponseResult result = new ResponseResult();
            //创建实例
            using (var db = new SyDbContext())
            {
                //开启事务
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //酒店
                        Hotel hotel = db.Hotel.Find(temporary.HotelId);
                        if (hotel!=null) 
                        {
                            hotel.HotelName = temporary.HotelName;
                            hotel.EndPlace = temporary.EndPlace;
                            hotel.TotelType = temporary.TotelType;
                            hotel.Level = temporary.Level;
                            hotel.Hotel_Tel = temporary.Hotel_Tel;
                            hotel.Describe = temporary.Describe;
                            hotel.Hotel_CreateTime = DateTime.Now;
                            hotel.Hotel_UpdateTime = DateTime.Now;
                            //修改酒店表
                            db.Entry(hotel).State = EntityState.Modified;
                        }

                        //酒店详细
                        HotelDetails hotelDetails = db.HotelDetails.Find(temporary.HotelDetails_Id);
                        if (hotelDetails!=null) 
                        {
                            hotelDetails.HotelId = hotel.HotelId;
                            hotelDetails.HotelAddress = temporary.HotelAddress;
                            hotelDetails.NumberOfRooms = temporary.NumberOfRooms;
                            hotelDetails.Hotel_BigPic = temporary.Hotel_BigPic;
                            hotelDetails.Hotel_SmallPic = temporary.Hotel_SmallPic;
                            hotelDetails.HotelDetails_CreateTime = DateTime.Now;
                            hotelDetails.HotelDetails_UpdateTime = DateTime.Now;
                            //修改酒店详情表
                            db.Entry(hotelDetails).State = EntityState.Modified;
                        }
                        //保存
                        db.SaveChanges();

                        //提交
                        tran.Commit();
                        result.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        result.Success = false;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 多表删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult DeleteHotel(int id)
        {
            ResponseResult result = new ResponseResult();
            Hotel hotel = new Hotel();
            hotel.HotelId = id;
            using (var db = new SyDbContext())
            {
                //开启事务
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        List<HotelDetails> hotelDetails = db.HotelDetails.Where(a => a.HotelId == id).ToList();
                        List<RoomInfo> roomInfos = db.RoomInfo.Where(a => a.HotelId == id).ToList();
                        List<HotelOrder> hotelOrders = db.HotelOrder.Where(a => a.HotelId == id).ToList();
                        //删除酒店表
                        db.Entry(hotel).State = EntityState.Deleted;

                        //循环删除当前酒店下的信息记录
                        for (int i = 0; i < hotelDetails.Count; i++)
                        {
                            //根据主键id获取单个实体
                            var entity = db.HotelDetails.Find(hotelDetails[i].HotelDetails_Id);
                            //如果实体不为空则进行删除
                            if (entity != null)
                            {
                                //删除
                                db.HotelDetails.Remove(entity);
                            }
                        }

                        //循环删除当前酒店下的房间信息
                        for (int i = 0; i < roomInfos.Count; i++)
                        {
                            //根据主键id获取单个实体
                            var entity = db.RoomInfo.Find(roomInfos[i].Room_Id);
                            //如果实体不为空则进行删除
                            if (entity != null)
                            {
                                //删除
                                db.RoomInfo.Remove(entity);
                            }
                        }

                        //循环删除当前酒店下的预订信息
                        for (int i = 0; i < hotelOrders.Count; i++)
                        {
                            //根据主键id获取单个实体
                            var entity = db.HotelOrder.Find(hotelOrders[i].HotelOrder_Id);
                            //如果实体不为空则进行删除
                            if (entity != null)
                            {
                                //删除
                                db.HotelOrder.Remove(entity);
                            }
                        }
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                        result.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        result.Success = false;
                    }
                }
            }
            return result;
        }
    }
}
